﻿using System;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using AssetAllocationSystem.Models;
using AssetAllocationSystem.Repositories;
using AssetAllocationSystem.Tests.RepositoriesFixtures;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;

namespace AssetAllocationSystem.Tests.Repositories
{
    [TestClass]
    public class AssetRepositoryFixture : FixtureBase
    {
        private static bool IsDataCalled = false;
        private static ISession session;

        public AssetRepositoryFixture()
        {
            session = CreateSession();
        }

        [TestInitialize()]
        public void SetUp()
        {
            if (!IsDataCalled)
            {
                for (int i = 0; i < 5; i++)
                {
                    var objAsset = new Asset();
                    objAsset.Id = Guid.NewGuid();
                    objAsset.ServiceKey = "ServiceKey";
                    objAsset.Location = new Location();
                    objAsset.Location.Code = "PrimaryLocation1";
                    objAsset.Location.Id = Guid.NewGuid();
                    objAsset.Configuration = "Configuration";
                    objAsset.Employee = null;
                    objAsset.Request = null;

                    session.Save(objAsset);
                    session.Flush();
                }

                var objAsset1 = new Asset();
                objAsset1.Id = new Guid("E224CE21-8870-41CA-A23A-9F3C01123D86");
                objAsset1.ServiceKey = "ServiceKey";
                objAsset1.Location = new Location();
                objAsset1.Location.Code = "PrimaryLocation1";
                objAsset1.Location.Id = Guid.NewGuid();
                objAsset1.Configuration = "Configuration";
                objAsset1.Employee = null;
                objAsset1.Request = null;

                session.Save(objAsset1);
                session.Flush();

                IsDataCalled = true;
            }
        }

        [TestCleanup()]
        public void CleanUp()
        {
            
        }

        [TestMethod]
        public void Check_if_Asset_Inserts_Fine()
        {
            var objAsset12 = new Asset();
            objAsset12.Id = Guid.NewGuid();
            objAsset12.ServiceKey = "ServiceKey";
            objAsset12.Location = new Location();
            objAsset12.Location.Code = "PrimaryLocation1";
            objAsset12.Location.Id = Guid.NewGuid();
            objAsset12.Configuration = "Configuration";
            objAsset12.Employee = null;
            objAsset12.Request = null;

            var assetRepository = new RepositoryBase<Asset>(session);
            var returnValue = assetRepository.CreateOrUpdate(objAsset12);

            Assert.AreEqual(true, returnValue);

            session.Delete(objAsset12);
            session.Flush();
        }

        [TestMethod]
        public void check_if_FindByServiceKey_name()
        {
            var assetRepository = new AssetRepository(session);
            var asset = assetRepository.FindByServiceKey("ServiceKey");

            Assert.IsNotNull(asset);
            Assert.AreEqual(6, asset.Count);
            Assert.AreEqual(asset[0].Configuration, "Configuration");
        }

        [TestMethod, Ignore]
        public void check_if_FindById()
        {
            var assetRepository = new AssetRepository(session);
            var asset = assetRepository.FindById(new Guid("E224CE21-8870-41CA-A23A-9F3C01123D86"));

            Assert.IsNotNull(asset);
            Assert.AreEqual(asset.Configuration, "Configuration");
        }
    }
}
